#include "stdafx.h"
#include "CuiDllNameToSame.h"

CuiDllNameToSame::CuiDllNameToSame()
{
}

CuiDllNameToSame::~CuiDllNameToSame()
{
}



bool CuiDllNameToSame::Do(CString fileName)
{

	ChangeFuncToArchi(fileName);

	return true;
}

void CuiDllNameToSame::ChangeFuncToArchi(CString fileName)
{
	PublicFunc::ReadSourceFile(fileName, m_csReadlines);


	CFileFind finder;
	BOOL bResult = finder.FindFile(fileName);
	if (bResult)
	{
		DeleteFile(fileName);
	}
	try
	{
		CString str;
		CStdioFile file(fileName, CFile::modeCreate | CFile::modeWrite);			file.SetLength(0);

		int num = this->m_csReadlines.GetSize();
		for (int k = 0; k < num; k++)
		{
			CString string = m_csReadlines[k], string1;

			int id1 = 0, id2 = 0;


			if (PublicFunc::IsStrHasStr(string, _T("Module=\"Trans\\program_hyarch2019\\"), id1, id2))
			{
				string1 = string;
				int ide = PublicFunc::ReverseFindString(string, _T(".dll"));
				if (ide > id2)
				{
					CString dllName = string.Mid(id2, ide - id2);
					string1.Replace(dllName, _T("RevitArchiModule2019.dll"));
				}
			}
			else
			{
				string1 = string;
			}

			file.WriteString(string1);
			if (k == num - 1)
			{
				int kkk = 0;
			}
			else
			{
				file.WriteString("\n");
			}
		}
		file.Close();
	}
	catch (CFileException* e)
	{
		e->ReportError();
		e->Delete();

		return;
	}
}


bool CuiDllNameToSame::IsFileValid(CString fileName)
{
	int id = fileName.ReverseFind('.');
	int len = fileName.GetLength();
	CString csExt = fileName.Mid(id + 1, len - id - 1);

	if (csExt == _T("CUI")) return true;

	return false;
}
